import { defineStore } from "pinia";
import { login, logout, getInfo } from '@/api/auth'
import { getToken, setToken, removeToken } from '@/utils/auth'
import useAppStore from './app'

const useUserStore = defineStore('user', () => {
    // 当前用户 ID
    const userId = ref(0)
    // 当前用户 TOKEN
    const token = ref(getToken())
    // 是否处于登录态
    const isLogin = ref(false)
    // 当前用户是否为管理员
    const isAdmin = ref(false)
    // 当前用户 ROLES
    const roles = ref([])
    // 当前用户 PERMS
    const permissions = ref([])
    // 当前模块
    const modules = ref([])
    // 登录操作
    function doLogin(params) {
        const username = params.username.trim()
        const password = params.password
        const code = params.code
        const uuid = params.uuid
        return new Promise((resolve, reject) => {
            login(username, password, code, uuid).then(res => {
                const appStore = useAppStore()
                appStore.resetApp()
                setToken(res.token)
                userId.value = res.id;
                token.value = res.token
                isLogin.value = true
                // 执行获取用户信息
                getProfile()
                resolve(res)
            }).catch(error => {
                console.log(error)
                reject(error)
            })
        })
    }
    // 登出操作
    function doLogout() {
        return new Promise((resolve, reject) => {
            logout(this.token).then(() => {
                removeToken()
                token.value = ''
                roles.value = []
                permissions.value = []
                isLogin.value = false
                isAdmin.value = false
                modules.value = []
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    }
    // 获取用户个人信息
    function getProfile() {
        return new Promise(function (resolve, reject) {
            getInfo().then(res => {
                console.log(res)
                isAdmin.value = res?.isAdmin || false
                permissions.value = res?.permissions || []
                roles.value = res?.roles || []
                modules.value = res?.modules || []
                resolve(res)
            }).catch(e => {
                reject(e)
            })
        })
    }
    /**
     * 是否拥有某种权限
     * @param {*} permission 
     * @return boolean
     */
    async function hasPermission(permission) {
        if(permissions.length == 0) return false;
        if (permissions.value.includes('*:*:*') || permissions.value.includes(permission) || isAdmin.value) {
            return true
        }
        return false
    }
    /**
     * 是否拥有某种角色标识
     * @param {*} role 
     * @return boolean
     */
    async function hasRole(role) {
        if (roles.value.includes(role) || isAdmin.value) {
            return true
        }
        return false
    }
    return { userId, token, isLogin, isAdmin, roles, permissions, modules, doLogin, doLogout,getProfile, hasPermission, hasRole }
}, {
    persist: {
        key: 'USER_STORE',
        storage: sessionStorage
    }
})
export default useUserStore